package com.qf.controller;

import com.qf.common.ResponseResult;
import com.qf.entity.Banner;
import com.qf.entity.Product;
import com.qf.request.RequestParams;
import com.qf.request.RequestProductParams;
import com.qf.service.ProductService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/*
   @author 86155
   @ClassName: com.qf.controller.ProductController
   @Description: 该类的功能描述
   @date: 2021/9/30 14:34 
*/
@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    private ProductService productService;

    @PostMapping("/add")
    public ResponseResult<Integer> addProduct(@RequestBody RequestProductParams requestProductParams) {
        try {
            int i = productService.addProduct(requestProductParams);
            if(i>0){
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    @PutMapping("/update")
    public ResponseResult<Integer> updateBanner(@RequestBody RequestProductParams requestProductParams){
        try {
            Integer i= productService.updateProduct(requestProductParams);
            if(i>0){
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    @DeleteMapping("/delete/{pid}")
    public ResponseResult<Integer> deleteProduct(@PathVariable("pid") Long pid){
        try {
            Integer i=  productService.deleteProduct(pid);
            if(i>0){
                return ResponseResult.success(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }

    @GetMapping("/find")
    public ResponseResult<List<Product>> findAllProduct(Integer status,String productName,
                                                      @RequestParam(defaultValue = "1") Integer page,
                                                      @RequestParam(defaultValue = "10") Integer size){
        try {
            List<Product>products = productService.selectAllProduct(status,productName,page,size);
            return ResponseResult.success(products);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.error();
    }
}
